<script setup lang="ts">
import { dateFormatter } from '@/utils/formatTime'
import { getJDSpuPage } from '@/api/mall/product/spu'
import { ref, watch } from 'vue'

defineOptions({ name: 'MaterialSelectBox' })
const emit = defineEmits<{
  confirm: [selectList: any]
}>()

const dialogVisible = ref(false) // 弹窗的是否展示
const loading = ref(true) // 列表的加载中
const total = ref(0) // 列表的总页数
const list = ref<any[]>([]) // 列表的数据
const queryParams = reactive({
  pageNo: 1,
  pageSize: 20,
  name: undefined,
  status: undefined,
  createTime: []
})
const queryFormRef = ref() // 搜索的表单
const tableRef = ref()
const selectedIdList: any = ref([]) // 选中的列表
/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await getJDSpuPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}
/** 搜索按钮操作 */
const handleQuery = () => {
  getList()
}

/** 打开物料选择弹窗 */
const open = async (id: number, selectedList?: any[]) => {
  dialogVisible.value = true
  // queryFormRef.value.resetFields()
  currentRow.value = {}
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 提交选择 */
const submitForm = async () => {
  try {
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('confirm', currentRow.value)
  } finally {
  }
}

const currentRow = ref({})
// 单选表格行
const handleCurrentChange = (val: any) => {
  currentRow.value = val
}
// 双击表格行
const handleCurrentChangeDb = (val: any) => {
  currentRow.value = val
  submitForm()
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗
</script>

<template>
  <Dialog v-model="dialogVisible" title="金蝶物料选择" width="1200">
    <ContentWrap>
      <el-form :model="queryParams" ref="queryFormRef" :inline="true" label-width="68px">
        <el-form-item label="物料名称" prop="name">
          <el-input
            v-model="queryParams.name"
            placeholder="请输入金蝶物料名称"
            clearable
            @keyup.enter="handleQuery"
            class="!w-240px"
          />
        </el-form-item>
        <el-form-item label="物料编码" prop="number">
          <el-input
            v-model="queryParams.number"
            placeholder="请输入金蝶物料编码"
            clearable
            @keyup.enter="handleQuery"
            class="!w-240px"
          />
        </el-form-item>
        <el-form-item>
          <el-button @click="handleQuery">
            <Icon icon="ep:search" class="mr-5px" />
            搜索
          </el-button>
          <el-button @click="resetQuery">
            <Icon icon="ep:refresh" class="mr-5px" />
            重置
          </el-button>
        </el-form-item>
      </el-form>
    </ContentWrap>
    <!-- 列表 -->
    <ContentWrap>
      <el-table
        ref="tableRef"
        v-loading="loading"
        highlight-current-row
        :data="list"
        row-key="id"
        height="500"
        @current-change="handleCurrentChange"
        @row-dblclick="handleCurrentChangeDb"
      >
        <el-table-column label="物料名称" prop="name" min-width="200" />
        <el-table-column label="物料编码" prop="number" min-width="150" />
        <el-table-column label="可用库存" prop="stock" />
        <el-table-column label="组织ID" prop="useOrgId" />
        <el-table-column
          label="修改时间"
          align="center"
          prop="externalModifyDate"
          width="180"
          :formatter="dateFormatter"
        />
      </el-table>
      <!-- 分页 -->
      <Pagination
        :total="total"
        v-model:page="queryParams.pageNo"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
      />
    </ContentWrap>
    <template #footer>
      <el-button :disabled="loading || !currentRow?.id" type="primary" @click="submitForm">
        确 定
      </el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>

<style scoped lang="scss"></style>
